// pages/call/call.js
let app = getApp();
const backgroundAudioManager = wx.getBackgroundAudioManager();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentUser: {}
  },

  bindStopBtn: function() {
    wx.redirectTo({
      url: '/pages/callList/callList',
    });
  },

  bindSureBtn: function() {
    wx.redirectTo({
      url: '/pages/chat/chat',
    });
    return false;

    let that = this;
    let params = {
      phoneNum: parseInt(that.data.currentUser.phoneNum),
      userid: (new Date()).getTime()
    };
    wx.setStorage({
      key: 'userid',
      data: params.userid,
    }); 
    // 发送当前接听的用户给后台，返回该用户的相关语音链接
    const url_callUser = app.globalData.baseUrl + '/chat';
    wx.showLoading({
      title: '接听中，请等待',
    })
    wx.request({
      url: url_callUser,
      method: 'post',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.status == 'Running') {
          wx.setStorage({
            key: 'curUserAudio',
            data: res.data,
          });
          wx.redirectTo({
            url: '/pages/chat/chat',
          })
        }
        
      },
      fail: function(error) {
        console.log('calling send msg', error.errMsg);
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;

    wx.getStorage({
      key: 'callUser',
      success: function(res) {
        that.setData({
          currentUser: res.data
        });
      },
    })

    backgroundAudioManager.src = 'https://voice-story.oss-cn-shanghai.aliyuncs.com/ivr/other/call.mp3'
  },
  onHide: function() {
    backgroundAudioManager.stop();
  },

  onUnload: function() {
    backgroundAudioManager.stop();
  }

})